package gold.digger;

import gold.utils.InputUtil;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC1109 {
    public long startExecuteTime = System.currentTimeMillis();


    public class DifferenceUtil {
        private int[] diff;

        public DifferenceUtil(int[] nums) {
            assert nums.length > 0;
            diff = new int[nums.length];
            // 构造差分数组
            diff[0] = nums[0];
            for (int i = 1; i < nums.length; i++) {
                diff[i] = nums[i] - nums[i - 1];
            }
        }

        // 闭区间[i,j]增加val
        public void increase(int i, int j, int val) {
            diff[i] += val;
            if (j + 1 < diff.length) diff[j + 1] -= val;
        }

        public int[] getResult() {
            int[] res = new int[diff.length];
            res[0] = diff[0];
            for (int i = 1; i < diff.length; i++) {
                res[i] = res[i - 1] + diff[i];
            }
            return res;
        }


    }

    class Solution {
        public int[] corpFlightBookings(int[][] bookings, int n) {
            int[] arr = new int[n];
            DifferenceUtil df = new DifferenceUtil(arr);
            for (int[] booking : bookings) {
                int i = booking[0] - 1;
                int j = booking[1] - 1;
                int val = booking[2];
                df.increase(i, j, val);
            }
            return df.getResult();
        }
    }

    public void run() {
        Solution solution = new Solution();
        int[][] bookings = InputUtil.toDoubleIntegerArray("[[1,2,10],[2,3,20],[2,5,25]]");
        System.out.println(solution.corpFlightBookings(bookings, 5));
    }

    public static void main(String[] args) throws Exception {
        LC1109 an = new LC1109();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
